package vashion.azeroth.core.manager.personal.config;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import vashion.azeroth.common.math.BigDecimalUtil;
import vashion.azeroth.core.dao.BaseDao;
import vashion.azeroth.core.exchangerate.ExchangeRateUtils;
import vashion.azeroth.core.item.pojo.ExchangeRateOfBank;
import vashion.azeroth.core.manager.impl.BaseManagerImpl;
import vashion.azeroth.core.personal.dao.ConfigExchangeRateDao;
import vashion.azeroth.core.personal.pojo.ConfigExchangeRate;

@Service
public class ConfigExchangeRateManager extends BaseManagerImpl<ConfigExchangeRate> {

	private final static Logger logger = LoggerFactory.getLogger(ConfigExchangeRateManager.class);

	@Autowired
	ConfigExchangeRateDao configExchangeRateDao;
	
	@Autowired
	ExchangeRateUtils exchangeRateUtils;

	@Override
	public BaseDao<ConfigExchangeRate> getDao() {
		return configExchangeRateDao;
	}

	public void updateExchangeRate(ConfigExchangeRate configExchangeRate) {
		List<ConfigExchangeRate> exchangeRates = configExchangeRateDao.findAll(configExchangeRate);

		for (ConfigExchangeRate exchangeRate : exchangeRates) {
			try {
				ExchangeRateOfBank rate = exchangeRateUtils.getExchangeRateOfBOC(exchangeRate.getCurno());
				exchangeRate.setSesell(new BigDecimal(rate.getSe_sell()));
				exchangeRate.setSebuy(new BigDecimal(rate.getSe_buy()));
				exchangeRate.setCnsell(new BigDecimal(rate.getCn_sell()));
				exchangeRate.setCnbuy(new BigDecimal(rate.getCn_buy()));
				exchangeRate.setMiddle(new BigDecimal(rate.getMiddle()));
				exchangeRate.setTimeEdit(new Date());
				configExchangeRateDao.update(exchangeRate);
			} catch (Exception e) {
				logger.error("汇率更新异常", e);
			}
		}
	}

	/**
	 * 将其它货币转换为人民币
	 * 
	 * @param curno
	 * @return
	 */
	public long transferToCNY(String curno, long price) {
		if ("cny".equalsIgnoreCase(curno)) {
			return price;
		}
		ConfigExchangeRate rate = configExchangeRateDao.get(curno);
		long prictCny = BigDecimalUtil.multiply(rate.getCnsell(), new BigDecimal(price)).divide(new BigDecimal(100))
				.setScale(0, BigDecimal.ROUND_HALF_UP).longValue();

		return prictCny;
	}

	public long transferToCNY(ConfigExchangeRate rate, long price) {
		long prictCny = BigDecimalUtil.multiply(rate.getCnsell(), new BigDecimal(price)).divide(new BigDecimal(100))
				.setScale(0, BigDecimal.ROUND_HALF_UP).longValue();

		return prictCny;
	}

	/**
	 * 将人民币转换为其它货币
	 * 
	 * @param curno
	 * @return
	 */
	public long transferFromCNY(String curno, long price) {
		if ("cny".equalsIgnoreCase(curno)) {
			return price;
		}
		ConfigExchangeRate rate = configExchangeRateDao.get(curno);
		long prictCurno = BigDecimalUtil.divide(new BigDecimal(price), rate.getCnsell()).multiply(new BigDecimal(100))
				.longValue();

		return prictCurno;
	}

	public long transferFromCNY(ConfigExchangeRate rate, long price) {
		long prictCurno = BigDecimalUtil.divide(new BigDecimal(price), rate.getCnsell()).multiply(new BigDecimal(100))
				.longValue();

		return prictCurno;
	}

	public BigDecimal getRate(String curno) {
		if ("cny".equalsIgnoreCase(curno)) {
			return new BigDecimal(100);
		}
		ConfigExchangeRate rate = configExchangeRateDao.get(curno);
		if (null != rate) {
			return rate.getCnsell();
		}

		return null;
	}

	public List<ConfigExchangeRate> findAllEnable() {
		return configExchangeRateDao.findAllEnable();
	}

}
